package com.google.bartenderserver;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

public class Stage extends View {
        public boolean shakeAnimationActive = false;
        public boolean pourAnimationActive = false;
        public boolean putAwayAnimationActive = false;
        public boolean devShowGrid = false;
        public Activity listener = null;
        public int activeBottle = 2;
        public int leftArmStatus;
        
        int shakeFrame = 0;
        int pourFrame = 0;
        int putAwayFrame = 0;
        boolean showGlass = false;
        
        boolean putAwayFlipped = false;
        int[] shakeFrames = {
                        R.drawable.putdown1,
                        R.drawable.putdown2,
                        R.drawable.putdown3,
                        R.drawable.putdown2,
                        R.drawable.putdown3,
                        R.drawable.putdown2,
                        R.drawable.putdown1,
                };
        
/*      int[] rightPourFrames = {
                        R.drawable.rightpour1,
                        R.drawable.rightpour2,
                        R.drawable.rightpour3,
                        R.drawable.rightpour4,
                        R.drawable.rightpour5,
                        R.drawable.rightpour4,
                        R.drawable.rightpour3,
                        R.drawable.rightpour2,
                        R.drawable.rightpour1,
                };*/
        
        int[][] rightPourFrames = {{
                        R.drawable.right11,
                        R.drawable.right12,
                        R.drawable.right13,
                        R.drawable.right14,
                        R.drawable.right15,
                        R.drawable.right14,
                        R.drawable.right13,
                        R.drawable.right12,
                        R.drawable.right11,
                },{
                        R.drawable.right21,
                        R.drawable.right22,
                        R.drawable.right23,
                        R.drawable.right24,
                        R.drawable.right25,
                        R.drawable.right24,
                        R.drawable.right23,
                        R.drawable.right22,
                        R.drawable.right21,
                },{
                        R.drawable.right31,
                        R.drawable.right32,
                        R.drawable.right33,
                        R.drawable.right34,
                        R.drawable.right35,
                        R.drawable.right34,
                        R.drawable.right33,
                        R.drawable.right32,
                        R.drawable.right31,
                }};     
        
        int[] leftPourFrames = {
                        R.drawable.leftpour1,
                        R.drawable.leftpour2,
                        R.drawable.leftpour3,
                        R.drawable.leftpour4,
                        R.drawable.leftpour5,
                        R.drawable.leftpour4,
                        R.drawable.leftpour3,
                        R.drawable.leftpour2,
                        R.drawable.leftpour1,
                };
        
        int[] putAwayFrames = {
                        R.drawable.putdown1,
                        R.drawable.putdown2,
                        R.drawable.putdown3, // Arm down
                        R.drawable.putdown4,
                        R.drawable.putdown5,
                        R.drawable.putdown6,
                        R.drawable.putdown7, // Arm up
                        R.drawable.putdown8,
                        R.drawable.putdown9,
                        R.drawable.putdown10, 
                        0, 
                        R.drawable.putdown10, // Glass up
                        R.drawable.putdown9,
                        R.drawable.putdown8, 
                        R.drawable.putdown7,
                        R.drawable.putdown6,
                        1,
                        R.drawable.armup2,
                        R.drawable.armup3,
                        R.drawable.armup4,
                        R.drawable.armup5,
                        0,
                        R.drawable.armup5,
                        R.drawable.armup4,
                        R.drawable.armup3,
                        R.drawable.armup2,
                        R.drawable.armup1,
                        R.drawable.putdown6,
                        R.drawable.putdown5,
                        R.drawable.putdown4,
                        R.drawable.putdown3,
                        R.drawable.putdown2,
                        R.drawable.putdown1,
                        1,
                };
        
        public Stage(Context context, AttributeSet attrs) {
                super(context, attrs);
                leftArmStatus = R.drawable.putdown1;
                // TODO Auto-generated constructor stub
        }
        
        public void setListener(BartenderServerActivity l)
        {
                listener = l;
        }

        @Override
        protected void onDraw(Canvas canvas) {
                // TODO Auto-generated method stub
                super.onDraw(canvas);
                Paint paint = new Paint();
                paint.setColor(Color.WHITE);
        
                if(devShowGrid)
                {
                        for(int i=0; i<canvas.getWidth(); i=i+25)
                        {
                                canvas.drawLine(i, 0, i+1, canvas.getHeight(), paint);
                        }
                        
                        for(int i=0; i<canvas.getHeight(); i=i+25)
                        {
                                canvas.drawLine(0, i, canvas.getWidth(), i+1, paint);
                        }
                }
                
                int bodyShift = 100;
                Bitmap torso = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.torso);
                canvas.drawBitmap(torso, 525+bodyShift, 0, paint);      
                
                if(pourAnimationActive)
                {
                        Bitmap rightarm = BitmapFactory.decodeResource(getContext().getResources(), rightPourFrames[activeBottle][pourFrame]);
                        canvas.drawBitmap(rightarm, -18+bodyShift, 195, paint);
                        Bitmap leftarm = BitmapFactory.decodeResource(getContext().getResources(), leftPourFrames[pourFrame]);
                        canvas.drawBitmap(leftarm, 650+bodyShift, 340, paint);
                        Bitmap counter = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.bartopgif);
                        canvas.drawBitmap(counter, 0, 0, paint);
                }
                else if(shakeAnimationActive)
                {
                        Bitmap bartender = BitmapFactory.decodeResource(getContext().getResources(), shakeFrames[shakeFrame]);
                        //canvas.drawBitmap(bartender, 760+bodyShift, 153, paint);
                        canvas.drawBitmap(bartender, 775+bodyShift, 75, paint);
                        Bitmap rightarm = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.rightarm4);
                        canvas.drawBitmap(rightarm, 0+bodyShift, 268, paint);
                        Bitmap counter = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.bartopgif);
                        canvas.drawBitmap(counter, 0, 0, paint);
                }
                else if(putAwayAnimationActive)
                {
                        int frame = putAwayFrames[putAwayFrame];
                        
                        Bitmap rightarm = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.rightarm4);
                        canvas.drawBitmap(rightarm, 0+bodyShift, 268, paint);
                        
                        if(frame == 0)
                        {
                                putAwayFlipped = !putAwayFlipped;
                                frame = putAwayFrames[putAwayFrame-1];
                        }
                        
                        if(frame == 1)
                        {
                                showGlass = !showGlass;
                                frame = putAwayFrames[putAwayFrame-1];
                        }
                        
                        if(putAwayFlipped)
                        {
                                Bitmap counter = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.bartopgif);
                                canvas.drawBitmap(counter, 0, 0, paint);
                                

                        }
                        
                        Bitmap leftarm = BitmapFactory.decodeResource(getContext().getResources(), frame);
                        canvas.drawBitmap(leftarm, 775+bodyShift, 75, paint);
                        
                        if(putAwayFlipped && showGlass)
                        {
                                Bitmap glass = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.glass);
                                canvas.drawBitmap(glass, 1010+bodyShift, 790, paint);
                        }
                        
                        if(!putAwayFlipped)
                        {
                                Bitmap counter = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.bartopgif);
                                canvas.drawBitmap(counter, 0, 0, paint);
                                
                                if(showGlass)
                                {
                                        Bitmap glass = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.glass);
                                        canvas.drawBitmap(glass, 1010+bodyShift, 790, paint);
                                }
                        }
                }               
                else
                {
                        Bitmap rightarm = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.rightarm4);
                        canvas.drawBitmap(rightarm, 0+bodyShift, 268, paint);
                        Bitmap leftarm = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.putdown1);
                        canvas.drawBitmap(leftarm, 775+bodyShift, 75, paint);                   
                        Bitmap counter = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.bartopgif);
                        canvas.drawBitmap(counter, 0, 0, paint);
                }

                if(shakeAnimationActive)
                {
                        animateShaker();
                }
                
                if(pourAnimationActive)
                {
                        animatePour();
                }
                
                if(putAwayAnimationActive)
                {
                        animatePutAway();
                }
        }
        
        public void animateShaker() {
                // TODO Auto-generated method stub

                shakeFrame++;
                
                if(shakeFrame == shakeFrames.length)
                {
                        shakeAnimationActive = false;
                        shakeFrame = 0;
                        
                        if(listener != null)
                        {
                                listener.setTitle("Ready...");
                        }
                }
                
                invalidate();   
        }
        
        public void animatePour() {
                // TODO Auto-generated method stub

                pourFrame++;
                
                if(pourFrame == rightPourFrames[activeBottle].length)
                {
                        pourAnimationActive = false;
                        pourFrame = 0;
                        
                        if(listener != null)
                        {
                                listener.setTitle("Ready...");
                        }
                }
                
                invalidate();   
        }
        
        public void animatePutAway() {
                // TODO Auto-generated method stub

                putAwayFrame++;
                
                if(putAwayFrame == putAwayFrames.length)
                {
                        putAwayAnimationActive = false;
                        putAwayFrame = 0;
                        leftArmStatus = R.drawable.leftarm;
                        putAwayFlipped = false;
                        showGlass = false;
                        
                        if(listener != null)
                        {
                                listener.setTitle("Ready...");
                        }
                }
                
                invalidate();   
        }
        
        public void devStartShake()
        {
                if(shakeAnimationActive)
                {
                        return;
                }
                
                shakeAnimationActive = true;
                shakeFrame = 0;
                invalidate();
        }
        
        public void devStartPour()
        {
                if(pourAnimationActive)
                {
                        return;
                }
                pourAnimationActive = true;
                pourFrame = 0;
                invalidate();
        }
        
        public void devPutAway()
        {
                if(putAwayAnimationActive)
                {
                        return;
                }
                putAwayAnimationActive = true;
                putAwayFrame = 0;
                invalidate();
        }
        
        
        public void devShowGrid()
        {
                devShowGrid = !devShowGrid;
                invalidate();
        }
        
        public void setItem(int i)
        {
                activeBottle = i;
                
        }
}
